<?php

/**
 * 挖宝 Service
 * 
 * Apps/Common/Service/WxEventTreasureAwardService.class.php
 * Zix @ 2015-08-26
 */

namespace Common\Service;

class WxEventTreasureAwardService {

  private static $instance;
  var $rank = array(
    1 => '一等奖',
    2 => '二等奖',
    3 => '三等奖',
    4 => '四等奖',
    5 => '五等奖',
    6 => '六等奖',
    7 => '七等奖',
    8 => '八等奖',
    9 => '九等奖',
  );
  var $valid_type = array(
    1 => 'SN'   ,
    2 => '实物' ,
  );

  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventTreasureAwardService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'id' => '',
      'event_type' => '',
      'event_id' => '',
      'rank' => '1',
      'name' => '',
      'valid_type' => '1',
      'min_point' => '500',
      'max_point' => '10000',
      'total_qty' => '1',
      'taken_qty' => '0',
      'remain_qty' => '1',
    );
  }

  function get_by_id($id) {
    $WxEventTreasureAward = M('WxEventTreasureAward');
    $data = $WxEventTreasureAward->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'status' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxEventTreasureAward = M('WxEventTreasureAward');

    $where = array();

    if (!empty($config['status'])) {
      $where['status'] = $config['status'];
    }

    if ($config['count']) {
      return $WxEventTreasureAward->where($where)->count();
    } else {
      $order = $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventTreasureAward
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $WxEventTreasureAward->_sql();
      return $data ? $data : array();
    }
  }

  function create($data, $is_ajax = true) {
    $WxEventTreasureAward = M('WxEventTreasureAward');
    $ret = $WxEventTreasureAward->add($data);
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr('添加成功', TRUE, array(
          'id' => $ret
        ));
      } else {
        return ajax_arr('添加失败', FALSE);
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }

  function update($id, $data) {
    $WxEventTreasureAward = M('WxEventTreasureAward');

    $ret = $WxEventTreasureAward->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventTreasureAward = M('WxEventTreasureAward');
    $ret = $WxEventTreasureAward->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

}
